package com.jbh.recordlibrary.v;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.google.gson.Gson;
import com.jbh.recordlibrary.u.ScreenUtil;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;

public class RecordWaveView extends View {

    /**
     * 默认绘制宽度为屏幕宽
     */
    private int defaultWidth ;
    /**
     * 默认绘制高度
     *  100dp
     *
     * @param context
     */
    private int defaultHeight ;
    /**
     * 绘制的view的宽高
     */
    private int viewWidth;
    private int viewHeight ;

    /**
     * 默认背景
     */
    private int defaultBgColor = Color.WHITE;



    private Paint mPaint ;


    private Canvas canvas ;

    /**
     * 绘制音频图形颜色
     *
     */
    private int recordLineColor = Color.GRAY;
    private int recordLineWidth = 4;


    private int centerLineColor = Color.GRAY ;
    private int centerLineHeight = 2 ;


    public void setCenterLineColor(int centerLineColor) {
        this.centerLineColor = centerLineColor;
    }


    public void setCenterLineHeight(int centerLineHeight) {
        this.centerLineHeight = centerLineHeight;
    }

    public RecordWaveView(Context context) {
        this(context,null);
    }


    public RecordWaveView(Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RecordWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        defaultWidth = ScreenUtil.w(getContext());
        defaultHeight = ScreenUtil.dpToPx(getContext(),100);
        setBackgroundColor(defaultBgColor);
        initDrawRecordPaint();
    }

    private void initDrawRecordPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(recordLineColor);
        mPaint.setStrokeWidth(recordLineWidth);
    }



    /**
     * 测量view大小
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(viewWidth = measureWidth(widthMeasureSpec),viewHeight=measureHeight(heightMeasureSpec));
    }




    /**
     * 测量高度
     *
     * @param heightMeasureSpec
     *
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int tempHeight = 0 ;

        if (heightMode == MeasureSpec.EXACTLY){
            tempHeight = heightSize ;
        }else {
            if (MeasureSpec.AT_MOST == heightMode){
                tempHeight = Math.min(heightSize,defaultHeight);
            }
        }
        return tempHeight ;
    }


    /**
     * 测量宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int measureWidth(int widthMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int tempWidth = 0 ;
        if (widthMode == MeasureSpec.EXACTLY){
            tempWidth = widthSize ;
        }else {
            if (MeasureSpec.AT_MOST == widthMode){
                tempWidth = Math.min(widthSize,defaultWidth);
            }
        }
        return tempWidth ;
    }



    /****************************************/
    /**           录音绘制图形部分          **/
    /****************************************/


    private List<Short> totalBuf ;
    private float divider ;


    /**
     *
     * @param totalBuf
     */
    public void setData(float divider,List<Short> totalBuf){
        this.totalBuf=totalBuf;
        this.divider=divider;
        Log.v("[RecordWaveView]","RecordWaveView = "+new Gson().toJson(totalBuf));
        invalidate();
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas ;
        drawTopLine(canvas);
        drawCenterLine(canvas);
        drawBottomLine(canvas);
        if (totalBuf!=null&&totalBuf.size()>0){
            try {
                drawRecord(canvas,divider,totalBuf);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }





    /**
     * 这里是绘制音频图形
     */
    public void drawRecord(Canvas canvas, float divider, List<Short> buf) throws JSONException {
        if (canvas!=null){
            //高低位交换
            for (int i = 0; i < buf.size(); i++) {
                byte bus[] = getBytes(buf.get(i));
                buf.set(i, (short)((0x0000 | bus[1]) << 8 | bus[0]));
            }

            int baseLine = getHeight()/2 ;
            for (int i = 0; i < buf.size(); i++) {
                double fb = buf.get(i) ;
                if (buf.get(i)<=5){
                    fb = 5 ;
                }
                float radio = (float) (fb *1.0 / 100);
                float y = radio * baseLine;
                /**当前指定位置绘制*/
                float currentDX = i * divider ;
                /**中间出波形*/
                canvas.drawLine(currentDX, baseLine-y, currentDX,baseLine + y, mPaint);
            }
        }
    }


    int line_off ;



    public void setLine_off(int line_off) {
        this.line_off = line_off;
        invalidate();
    }



    /**
     *
     * 中心线
     *
     * @param canvas
     */
    private void drawCenterLine(Canvas canvas) {
        Paint centerLine =new Paint();
        centerLine.setColor(centerLineColor);
        centerLine.setStrokeWidth(centerLineHeight);
        centerLine.setAntiAlias(true);
        centerLine.setDither(true);
        canvas.drawLine(0, getHeight()/2, getWidth() ,getHeight()/2, centerLine);
    }





    /**
     *
     * 最下面的那根线
     *
     * @param canvas
     */
    private void drawBottomLine(Canvas canvas) {
        Paint paintBottomLine =new Paint();
        paintBottomLine.setColor(recordLineColor);
        paintBottomLine.setStrokeWidth(recordLineWidth);
        paintBottomLine.setAntiAlias(true);
        paintBottomLine.setDither(true);
        canvas.drawLine(0, getHeight() - line_off/2, getWidth(), getHeight()-line_off/2, paintBottomLine);
    }





    /**
     *
     * 最上面的那根线
     *
     * @param canvas
     */
    private void drawTopLine(Canvas canvas) {
        Paint paintTopLine =new Paint();
        paintTopLine.setColor(recordLineColor);
        paintTopLine.setStrokeWidth(recordLineWidth);
        paintTopLine.setAntiAlias(true);
        paintTopLine.setDither(true);
        canvas.drawLine(0, line_off/2, getWidth(), line_off/2, paintTopLine);
    }






    /**
     * 高低位交换
     *
     * @param s
     * @return
     */
    public byte[] getBytes(short s) {
        byte[] buf = new byte[2];
        for (int i = 0; i < buf.length; i++) {
            buf[i] = (byte) (s & 0x00ff);
            s >>= 8;
        }
        return buf;
    }


}
